package com.wmlive.hhvideo.heihei.channel;

import android.text.TextUtils;

import com.wmlive.hhvideo.common.base.BasePresenter;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.beans.login.MobileLoginResponse;
import com.wmlive.hhvideo.heihei.beans.splash.InitCatchData;
import com.wmlive.hhvideo.utils.ToastUtil;
import com.wmlive.networklib.entity.DCNetObserver;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.wmlive.hhvideo.R;

/**
 * Created by XueFei on 2017/6/7.
 * <p>
 * 微信登陆
 */

public class LoginWechatChannelPresenter extends BasePresenter<IWechatLoginView> {

    public LoginWechatChannelPresenter(IWechatLoginView view) {
        super(view);
    }

    public void WXLogin() {
        Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
        if (null == wechat) {
            if (null != viewCallback) {
                viewCallback.handleWechatLoginFailure("登录失败");
            }
            return;
        }
        wechat.removeAccount(true);
        if (!wechat.isClientValid()) {
            ToastUtil.showToast(R.string.share_wechat_no_installed);
            viewCallback.handleWechatLoginFailure("");
            return;
        }
        wechat.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
                if (action == Platform.ACTION_USER_INFOR) {
                    PlatformDb platDB = platform.getDb();
                    if (null != platDB) {
                        String token = platDB.getToken();
                        String openId = platDB.getUserId();
                        String unionid = "";
                        if (hashMap != null) {
                            unionid = (String) hashMap.get("unionid");
                        }
                        getUserInfoFromWechatLogin(token, openId, unionid);
                    }
                }
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                if (null != viewCallback) {
                    viewCallback.handleWechatLoginFailure("登录失败");
                }
            }

            @Override
            public void onCancel(Platform platform, int i) {
                if (null != viewCallback) {
                    viewCallback.handleWechatLoginFailure("取消登录");
                }
            }
        });
        wechat.SSOSetting(false);
//        wechat.authorize();
        wechat.showUser(null);
    }

    private void getUserInfoFromWechatLogin(String account_token, String open_id, String unionid) {
        String account_source = "Wechat";
        String url = InitCatchData.userSignIn();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_LOGIN_WECHAT, getHttpApi().signIn(url, account_source, account_token, open_id, unionid))
                    .subscribe(new DCNetObserver<MobileLoginResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, MobileLoginResponse response) {
                            if (null != viewCallback) {
                                viewCallback.handleWechatLoginSucceed(response);
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (null != viewCallback) {
                                viewCallback.handleWechatLoginFailure(message);
                            }
                        }
                    });
        } else if (null != viewCallback) {
            viewCallback.handleNetError();
        }
    }
}
